﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RTSoft.SoureCode.Control.Bac;
using RTSoft.Modules;

namespace RTSoft.SoureCode.Entity
{
    public partial class frmBac : DevExpress.XtraEditors.XtraForm
    {
        public frmBac()
        {
            InitializeComponent();
        }
        DataTable tbl;
        String errConnect;
        String errSQL;

        private void frmBac_Load(object sender, EventArgs e)
        {
            try
            {
                this.InitGrid();
            }
            catch
            {
                Library.ShowMessage("Lỗi dữ liệu đầu vào!");
            } 
        }

        public void InitGrid()
        {
            tbl = new DataTable();
            errConnect = "";
            errSQL = "";
            bacctrl ctrl = new bacctrl();
            ctrl.getData(ref errConnect, ref errSQL, ref tbl);
            GridControl1.DataSource = tbl;
            GridView1.Columns["ID_BAC"].Caption = "";
            GridView1.Columns["BAC"].Caption = "Bậc";
           // GridView1.Columns["BAC"].Visible = false;
            GridView1.Columns["ID_BAC"].Visible = false;
            GridView1.BestFitColumns();
        }
        #region "Event Button"
        private void btnExcel_Click(object sender, EventArgs e)
        {
            //frmBac frm = new frmBac();
            Library.ExportToExcel(this, "Bậc", GridView1);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (GridView1.FocusedRowHandle >= 0)
            {
                int id = Convert.ToInt32(GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["ID_BAC"].FieldName));
                errConnect = "";
                errSQL = "";
                string strName = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["BAC"].FieldName).ToString();
                Boolean result = Library.ShowQuestionMessage("Bạn có chắc chắn xóa " + strName + " ?");
                if (result == false)
                {
                    return;
                }
                bacctrl ctrl = new bacctrl();
                ctrl.Delete(ref errConnect, ref errSQL, id);
                if (errSQL != "")
                {
                    Library.ShowMessage("Không thể xóa bậc này");
                }
                else
                {
                    GridView1.DeleteSelectedRows();
                    GridView1.RefreshData();
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void GridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            bacctrl ctrl = new bacctrl();
            bacinfo info = new bacinfo();

            errConnect = "";
            errSQL = "";
            DataRow row = GridView1.GetDataRow(e.RowHandle);
            info.ID_BAC = Convert.ToInt32(RTValidate.CheckNull(row["ID_BAC"]));
            info.BAC = RTValidate.CheckNull(row["BAC"]).ToString();
            if (info.ID_BAC != 0)
            {
                Boolean result = Library.ShowQuestionMessage("Dữ liệu thay đổi bạn có chắc chắn sửa");
                if (result != false)
                {
                    ctrl.Update(ref errConnect, ref errSQL, info);
                }
            }
            else
            {
                ctrl.Insert(ref errConnect, ref errSQL, info);
            }
            this.InitGrid();  
        }

      
    }
}